package com.xinqi.common.base.utils.chars;

import com.google.common.base.CaseFormat;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Converter;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.io.CharStreams;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 定义常用的字符与常用的分割和合并符
 *
 * @author linkai
 * @date 2022/11/25
 */
@SuppressWarnings("unused")
public interface Chars {

    char DOT = '.';

    char COMMA = ',';

    char EQUAL = '=';

    char CN_COMMA = '，';

    char SLASH = '/';

    static String toString(InputStream is) throws IOException {
        return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
    }

    static int startWithAny(String s, String... matches) {
        if (matches == null) {
            return -1;
        }
        int i = 0;
        for (String match : matches) {
            if (s.startsWith(match)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    static int startWithAny(String s, Iterable<String> matches) {
        if (matches == null) {
            return -1;
        }
        int i = 0;
        for (String match : matches) {
            if (s.startsWith(match)) {
                return i;
            }
            i++;
        }
        return -1;
    }

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isChinese(CharSequence s) {
        return s.codePoints().allMatch(Chars::isChinese);
    }

    static boolean isChinese(int c) {
        return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }

    static CharMatcher upperChineseNumberMatcher() {
        return Holder.UPPER_CHINESE_NUMBER_MATCHER;
    }

    static CharMatcher lowerChineseNumberMatcher() {
        return Holder.LOWER_CHINESE_NUMBER_MATCHER;
    }

    static CharMatcher chineseNumberMatcher() {
        return Holder.CHINESE_NUMBER_MATCHER;
    }

    static Converter<String, String> upperCamelToUnderscoreConverter() {
        return Holder.UPPER_CAMEL_UNDERSCORE;
    }

    @Deprecated
    static Splitter commaSplitter() {
        return Holder.SP_COMMA;
    }

    static Joiner dotJoiner() {
        return Holder.JO_DOT;
    }

    static Joiner commaJoiner() {
        return Holder.JO_COMMA;
    }

    static Joiner.MapJoiner commaEqualMapJoiner() {
        return Holder.JO_COMMA_EQUAL;
    }

    static Joiner equalJoiner() {
        return Holder.JO_EQUAL;
    }

    @Deprecated
    static Splitter chineseCommaSplitter() {
        return Holder.SP_CN_COMMA;
    }

    static Joiner chineseCommaJoiner() {
        return Holder.JO_CN_COMMA;
    }

    final class Holder {

        private static final Splitter SP_COMMA = Splitter.on(COMMA);

        private static final Joiner JO_DOT = Joiner.on(DOT);

        private static final Joiner JO_COMMA = Joiner.on(COMMA);

        private static final Joiner.MapJoiner JO_COMMA_EQUAL = commaJoiner().withKeyValueSeparator("=");

        private static final Joiner JO_EQUAL = Joiner.on(EQUAL);

        private static final Splitter SP_CN_COMMA = Splitter.on(CN_COMMA);

        private static final Joiner JO_CN_COMMA = Joiner.on(CN_COMMA);

        private static final Converter<String, String> UPPER_CAMEL_UNDERSCORE = CaseFormat.UPPER_CAMEL
            .converterTo(CaseFormat.UPPER_UNDERSCORE);

        private static final CharMatcher UPPER_CHINESE_NUMBER_MATCHER = CharMatcher.anyOf("零壹贰叁肆伍陆柒捌玖拾佰仟万亿");

        private static final CharMatcher LOWER_CHINESE_NUMBER_MATCHER = CharMatcher.anyOf("〇一二三四五六七八九十百千万亿");

        private static final CharMatcher CHINESE_NUMBER_MATCHER = CharMatcher.anyOf("零壹贰叁肆伍陆柒捌玖拾佰仟万亿〇一二三四五六七八九十百千万亿");

    }

}
